package entidade.caminhoneiro;

import tcc.caminhoneiro.R;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import apresentacao.caminhoneiro.Activity_Menu_Principal;

public class Local implements LocationListener{
	
	// distancia em 1*10 metros
    private static final float MIN_DISTANCE_CHANGE_FOR_UPDATES = 50; // 500 metros

    // tempo minutos
    private static final long MIN_TIME_BW_UPDATES = 1; // 1 minuto
    
    private Context context;
    private String locationProvider;
    
    private LocationManager lManager;
    
    public Local(Context context){
    	this.context = context;
    }
    
    private String getBestProvider(){
    	if (lManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
    		return LocationManager.NETWORK_PROVIDER;
    	}
    	return LocationManager.GPS_PROVIDER; 
    }
    
    public void setLocationprovider(String prov){
    	locationProvider = prov;
    }

    public void startGPS(){
    	
    	lManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        LocationListener lListener = new LocationListener() {
            public void onLocationChanged(Location locat) {
                updateView(locat);
            }
            public void onStatusChanged(String provider, int status, Bundle extras) {
            	
            }
            public void onProviderEnabled(String provider) {
            	
            }
            public void onProviderDisabled(String provider) {
            	
            }
        };
        locationProvider = getBestProvider(); 
        lManager.requestLocationUpdates(locationProvider,MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, lListener);
        //lManager.requestLocationUpdates(locationProvider, LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, lListener);
        Activity_Menu_Principal.localAtual = lManager.getLastKnownLocation(locationProvider);
    }
    
    public void updateView(Location locat){
    	Activity_Menu_Principal.localAtual = locat;
    }
    
	public Location getLocation(){
		LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		boolean ok = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
		if (ok){
		 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
	     //locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);	
	     
		 if (locationManager!=null){
		  String locationProvider = LocationManager.GPS_PROVIDER;
		  return locationManager.getLastKnownLocation(locationProvider);
		 }
		}
		return null;
	}
	
	public boolean isOk(){
		LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		
		return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
	}
	
	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

}
